<template>
  <view>
    <!-- 搜索栏 -->
    <view class="search-box">
      <uni-search-bar @confirm="search" @input="input" cancelButton='none' radius='18'></uni-search-bar>
    </view>
    <!-- 关键字商品推荐 -->
    <view class="list-item" v-for="(item,i) in searchList" :key='i' @click="gotoDetail(item)" v-if="searchList&&keyword!==''">
      <text>{{item.goods_name}}</text>
      <uni-icons type="right" size="17" class="right-arrow"></uni-icons>
    </view>
    <!-- 搜索历史 -->
    <view class="history" v-if="oldKeyword.length!==0&&keyword===''">
      <view class="list-item">
        <text>历史记录</text>
        <uni-icons type="trash" size="24" class="right-arrow"  @click="delOldKeyword"></uni-icons>
      </view>
      <view class="history-bottom">
        <view class="history-item" v-for="(kw,i) in oldKeyword" :key="i" @click="gotoGoodsList(kw)">
          {{kw}}
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer:null,
        keyword:'',
        oldKeyword:[],
        searchList:[]
      };
    },
    methods:{
      input(e){
        clearTimeout(this.timer)
        this.timer=setTimeout(()=>{
          this.keyword=e
          this._getSearchList()
        },500)
      },
      search(e){
        console.log(e)
      },
      // 根据输入关键字获取推荐商品
      async _getSearchList(){
        if(this.keyword===''){
          this.searchList=[]
          return 
        }
        this._saveSearchHistory()
        const {data:res}=await uni.$http.get('/api/public/v1/goods/qsearch',{query:this.keyword})
        if(res.meta.status!==200)return
        this.searchList=res.message
      },
      // 点击推荐商品item项跳转到对应商品
      gotoDetail(item){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id
        })
      },
      // 保存搜索历史记录
      _saveSearchHistory(){
        if(this.keyword!==''){
          this.oldKeyword.unshift(this.keyword)
        }
        this.oldKeyword=[...new Set(this.oldKeyword)]
        uni.setStorageSync('oldKey',JSON.stringify(this.oldKeyword))
      },
      // 清除历史关键字
      delOldKeyword(){
        this.oldKeyword.length=0
        this._saveSearchHistory()
        uni.removeStorageSync('oldKey')
      },
      // 历史记录点击事件
      gotoGoodsList(kw){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?query='+kw
        })
      }
    },
    onLoad(){
      this.oldKeyword=JSON.parse(uni.getStorageSync('oldKey')||'[]')
    }
  }
</script>

<style lang="scss">
  // 搜索栏
.search-box{
  position: sticky;
  top: 0;
  z-index: 999;
  background-color: #E16531;
}
// 关键字商品推荐
.list-item{
  position: relative;
  line-height: 40px;
  margin: 5px 10px;
  border-bottom: 1px solid #eee;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding-right: 1em;
  .right-arrow{
    position: absolute;
    top: 0;
    right: 0;
  }
}
// 搜索历史
.history{
  .history-bottom{
    margin: 0 10px;
    display: flex;
    flex-wrap: wrap;
    .history-item{
      padding: 2px 8px;
      border-radius: 10px;
      background-color: #eee;
      text-align: center;
      margin: 3px 4px 0 0;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      font-size: 13px;
    }
  }
}
</style>
